using System;
using Sinacor.OMM.Translator.Interfaces;
using Sinacor.OMM.MessageEntity;
using System.Text;

namespace Sinacor.OMM.Translator.Translators.STM
{
    public class Message0138 : BaseTranslator, ITranslator
    {
        private static Message0138 _instance = null;

        private Message0138()
        {
        }

        public static Message0138 GetInstance()
        {
            if (_instance == null)
                _instance = new Message0138();

            return _instance;
        }

        #region ITranslator Members

        public Sinacor.OMM.MessageEntity.BaseMessage TranslateToRouterMessage()
        {
            return TranslateToMessage();
        }

        public Sinacor.OMM.MessageEntity.BaseMessage TranslateToProcessMessage()
        {
            throw new System.NotImplementedException();
        }

        #endregion


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private Sinacor.OMM.MessageEntity.BaseMessage TranslateToMessage()
        {
            MessageEntity.STM.Message0138 _message = new Sinacor.OMM.MessageEntity.STM.Message0138();
            MessageEntity.BaseMessage baseMessage;
            int clientId;
            byte clientDigit;
            int port = 0;

            _message.MegaBolsaIdentificator = Sinacor.OMM.MessageEntity.Enum.MessagesMegaBolsaSource.OrdemEliminada;
            _message.Source = Sinacor.OMM.MessageEntity.Enum.MessageSource.MegaBolsa;
            _message.Stock = OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0138.Stock.StartIndex, LayoutCatalog.STM.Message0138.Stock.Length).Trim();

            if (int.TryParse(OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0138.Operator.StartIndex, LayoutCatalog.STM.Message0138.Operator.Length), out port))
                _message.Operator = port;

            AdjustClient(OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0138.Client.StartIndex, LayoutCatalog.STM.Message0138.Client.Length), out clientId, out clientDigit);
            _message.Client = clientId;

            switch (OriginalMessage.Body.Substring(LayoutCatalog.STM.Message0138.NaturezaOperacao.StartIndex, LayoutCatalog.STM.Message0138.NaturezaOperacao.Length))
            {
                case "A":
                    _message.NaturezaOperacao = Sinacor.OMM.MessageEntity.Enum.NaturezaOperacao.Compra;
                    break;
                case "V":
                    _message.NaturezaOperacao = Sinacor.OMM.MessageEntity.Enum.NaturezaOperacao.Venda;
                    break;
            }

            _message.Original = OriginalMessage.Body;

            baseMessage = TranslateMessageLabel(OriginalMessage.Label);
            _message.SequentialID = baseMessage.SequentialID;
            _message.ID = baseMessage.ID;
            _message.OperationDate = GetOperationDate(_message.ID);

            return _message;
        }
    }
}